package auctionhaus
//Authors: Paul Kleczka, Adam Backstrom

class Bid {

    float amount
    Date bidDateTime
    Listing listing

    static belongsTo = [listing: Listing]
    Customer bidder

    static namedQueries = {
        bidsByListing{param ->
            listing {
                eq 'id' , param.id
            }

        }
    }



    static constraints = {

        amount min: 0.01F, validator: {
            val, obj ->

            if(obj.listing)
            {
                Listing savedListing = Listing.get(obj.listing.id);
                if(savedListing.bids)
                {
                    Bid currentHighBid = savedListing.bids.get(savedListing.bids.size()-1)
                    if(currentHighBid.id && currentHighBid.id != obj.id)
                        return ((val >= (currentHighBid.amount + 0.5f)) && !obj.listing.completed)
                    else
                        return !obj.listing.completed
                }
                else
                {
                    //check if its over the starting bid price- first bid
                    return ((val >= savedListing.startingBidPrice) && !obj.listing.completed)
                }
            }
            else
            {
               return false
            }
        }
    }
}

